package model.beans;

import core.Database;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.log4j.Logger;

/**
 *
 * @author Danon
 */
@ManagedBean @RequestScoped
public class RouterBean implements Serializable {

    private static final Logger logger = Logger.getLogger(RouterBean.class);
    
    public static List<String> listByName(String name) {
        List<String> lst = new ArrayList<>();
        if(name==null || name.isEmpty()) return lst;
        try {
            Connection c = Database.getInstance().getConnection();
            try (PreparedStatement ps = c.prepareStatement("select title from routers where upper(title) LIKE upper(?||'%') and rownum <= 20")) {
                ps.setString(1, name);
                ResultSet rs = ps.executeQuery();
                while(rs.next()) {
                    lst.add(rs.getString("title"));
                }
                rs.close();
            }
        } catch (Exception ex) {
            logger.debug("Failed to obtain routers with the name starting with "+name+": "+ex.getMessage());
        }
        return lst;
    }
    
    public static RouterBean loadById(int id) {
        RouterBean r = null;
        try {
            Connection c = Database.getInstance().getConnection();
            try (PreparedStatement ps = c.prepareStatement("select router_id, title from routers where router_id = ?")) {
                ps.setInt(1, id);
                ResultSet rs = ps.executeQuery();
                if(rs.next()) {
                    r = new RouterBean(rs.getInt("router_id"), rs.getString("title"));
                }
            }
        } catch (Exception ex) {
            logger.debug("Failed to obtain router with id "+id+": "+ex.getMessage());
        }
        return r;
    }
    
    public static RouterBean loadByName(String name) {
        RouterBean r = null;
        try {
            Connection c = Database.getInstance().getConnection();
            try (PreparedStatement ps = c.prepareStatement("select router_id, title from routers where title = ?")) {
                ps.setString(1, name);
                ResultSet rs = ps.executeQuery();
                if(rs.next()) {
                    r = new RouterBean(rs.getInt("router_id"), rs.getString("title"));
                }
            }
        } catch (Exception ex) {
            logger.debug("Failed to obtain router with name "+name+": "+ex.getMessage());
        }
        return r;
    }
    
    public final long serialVersionID = 1L;
    
    private int id;
    private String name;

    public RouterBean() {}
    
    public RouterBean(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public boolean equals(Object o) {
        if(!(o instanceof RouterBean)) return false;
        RouterBean r = (RouterBean)o;
        return r.getId() == getId();
    }

    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + this.id;
        return hash;
    }
    
    @Override
    public String toString() {
        return ""+getId();
    }
    
    public List<LinkBean> listLinks() {
        return DirectedGraphBean.getIncidentVertices(this, 0);
    }
}
